﻿using System;
using System.Data;
using System.Windows.Forms;
using Janus.Windows.UI.Dock;
using Spring.Context;
using Spring.Context.Support;
using YiLe.ApplicationFolders;

namespace YiLe.UI
{
    public abstract class ApplicationFolder
    {
        protected FolderLoadMode mLoadMode = FolderLoadMode.CurrentFolder;

        protected string mFolderName;
        protected string mEditImageKey = "EditCustomer";
        protected string mNewImageKey = "NewItem";
        protected FolderFeatures mSupportedFeatures;

        private ApplicationFolderCollection mFolders;
        private FolderViewCollection mViews;
        private FolderView mCurrentView;
        private ContentViewControl mContentViewControl;
        private DataControl mPreviewControl;
        private string mImageKey;
        private DataProvider mDataProvider;
        private Janus.Windows.UI.Dock.UIPanelBase mNavigatorPanel;
        private ApplicationFolder mParentFolder;

        #region protected methods
        //Derived classes can override this method to fill the FolderViews
        //collection as needed. By default, this method loads the views from the
        //controls used by the folder or from the settings stored in the settings file.
        protected virtual void LoadFolderViews()
        {
            bool viewsLoaded = false;
            if (YiLeSystemApp.ReadSettingsFromDS)
            {
                DataRow[] views = null;
                views = YiLeSystemApp.ConfigDataSet.FolderViews.Select("ParentFolder = '" + this.GetType().FullName + "'");
                if (views.Length > 0)
                {
                    foreach (ConfigDataSet.FolderViewsRow row in views)
                    {
                        FolderView newView = new FolderView(row.ViewName, (ContentViewType) row.ContentViewType,
                                                            row.LayoutName);
                        newView.ReadingPaneLocation = (ReadingPaneLocation)row.ReadingPaneLocation;
                        mViews.Add(newView);
                    }
                    viewsLoaded = true;
                }
            }

            if (!viewsLoaded)
            {
                //load all possible views and settings form the layouts defined at a design time
                LoadViewsFromControl(ContentViewType.DashBoard);
                LoadViewsFromControl(ContentViewType.GridView);
                LoadViewsFromControl(ContentViewType.ScheduleView);
                LoadViewsFromControl(ContentViewType.TimelineView);
            }

        }

        //Derived classes must override this method and return the type
        //of the Control used to preview data in the main form if they support
        //ReadingPane
        protected virtual Type GetPreviewControlType
        {
            get 
            { 
                return null;
            }
        }

        //Derived classes must override this method and return the type
        //of the ContentViewControl used to present its data in the main form
        protected virtual Type GetTypeOfView(ContentViewType viewType)
        {
            return null;
        }

        //Derived classes must override this method to create the
        //appropriate data provider
        protected virtual DataProvider CreateDataProvider()
        {
            return null;
        }

        protected ContentViewControl CreateContentViewControl(ContentViewType viewType)
        {
            Type controlType = this.GetTypeOfView(viewType);
            if (controlType != null)
            {
                ContentViewControl ctl = null;
                ctl = (ContentViewControl)(Activator.CreateInstance(controlType));
                ctl.LoadSettings();
                return ctl;
            }
            else
            {
                return null;
            }
        }

        protected virtual void CreateContentViewControl()
        {
            if (CurrentView != null)
            {
                this.mContentViewControl = this.CreateContentViewControl(this.CurrentView.ContentViewType);
                mContentViewControl.LoadSettings();
                if (this.DataProvider != null)
                {
                    mContentViewControl.DataProvider = this.DataProvider;
                }
            }
        }

        protected virtual void ReleaseContentView()
        {
            if (mContentViewControl != null)
            {
                if (mContentViewControl.Parent != null)
                {
                    //Dispose the control only if it is free
                    //if the control has a parent, just release the reference and
                    //let its parent form dispose it.
                    mContentViewControl.Dispose();
                }
                mContentViewControl = null;
            }
        }

        protected void AddChildFolder(ApplicationFolder folder)
        {
            this.Folders.Add(folder);
            folder.mParentFolder = this;
        }

        #endregion

        #region Private method
        private void LoadViewsFromControl(ContentViewType viewType)
        {
            ContentViewControl viewControl = this.CreateContentViewControl(viewType);
            if (viewControl != null)
            {
                //get an array with defined views
                FolderView[] gridViewFolders = viewControl.GetDefinedViews();
                foreach (FolderView fView in gridViewFolders)
                {
                    if (this.Supports(FolderFeatures.ReadingPane))
                    {
                        fView.ReadingPaneLocation = ReadingPaneLocation.Bottom;
                    }
                    else
                    {
                        fView.ReadingPaneLocation = ReadingPaneLocation.Off;
                    }
                    mViews.Add(fView);
                }
                viewControl.Dispose();
            }
        }

        #endregion

        #region Public Properties
        /// <summary>
        /// folder文件名
        /// </summary>
        public string FolderName
        {
            get { return mFolderName; }
            set { mFolderName = value; }
        }
        /// <summary>
        /// 编辑image
        /// </summary>
        public string EditImageKey
        {
            get { return mEditImageKey; }
            set { mEditImageKey = value; }
        }
        /// <summary>
        /// 新增image
        /// </summary>
        public string NewImageKey
        {
            get { return mNewImageKey; }
            set { mNewImageKey = value; }
        }
        /// <summary>
        /// folders 应用程序文件夹视图集合
        /// Inheritors are responsible of filling the Folder collection when they are initialized.
        /// </summary>
        public ApplicationFolderCollection Folders
        {
            get
            {
                if (mFolders == null)
                {
                    mFolders = new ApplicationFolderCollection();
                }
                return mFolders;
            }
        }
        /// <summary>
        /// Views 文件视图集合
        /// Inheritors are responsible of filling the Views collection when they are initialized.
        /// </summary>
        public FolderViewCollection Views
        {
            get
            {
                if (mViews == null)
                {
                    mViews = new FolderViewCollection();
                    LoadFolderViews();
                }
                return mViews;
            }
        }
        /// <summary>
        /// 预览视图
        /// </summary>
        public virtual DataControl PreviewControl
        {
            get
            {
                if (mPreviewControl == null)
                {
                    Type controlType = this.GetPreviewControlType;
                    if (controlType != null)
                    {
                        mPreviewControl = (DataControl)(Activator.CreateInstance(controlType));
                    }
                    else
                    {
                        if (this.Supports(FolderFeatures.ReadingPane))
                        {
                            throw new NotImplementedException(this.GetType().FullName + " must implement GetPreviewControlType method if ReadingPane is supported.");
                        }
                    }
                }
                return mPreviewControl;
            }
        }
        /// <summary>
        /// 关闭预览视图
        /// </summary>
        public virtual void ReleasePreviewControl()
        {
            if (mPreviewControl != null)
            {
                if (mPreviewControl.Parent == null)
                {
                    mPreviewControl.Dispose();
                }
                mPreviewControl = null;
            }
        }
        /// <summary>
        /// 当前view
        /// </summary>
        public FolderView CurrentView
        {
            get
            {
                if (mCurrentView == null)
                {
                    this.CurrentView = this.DefaultView;
                }
                return mCurrentView;
            }
            set
            {
                if (value != null && !mViews.Contanis(value))
                {
                    throw new ArgumentException("View is not defined for the folder.");
                }
                if (mCurrentView != value)
                {
                    if (mContentViewControl != null)
                    {
                        if (mContentViewControl.GetType() != GetTypeOfView(value.ContentViewType))
                        {
                            //release content view only if the type of the content view
                            //is of a different type than the one already loaded
                            ReleaseContentView();
                        }
                    }

                }
                mCurrentView = value;
            }
        }
        /// <summary>
        /// 图片image
        /// </summary>
        public string ImageKey
        {
            get { return mImageKey; }
            set { mImageKey = value; }
        }
        /// <summary>
        /// 是否支持feature
        /// </summary>
        /// <param name="feature"></param>
        /// <returns></returns>
        public bool Supports(FolderFeatures feature)
        {
            return (mSupportedFeatures & feature) == feature;
        }
        /// <summary>
        /// LoadMode，文件夹load模式
        /// </summary>
        public FolderLoadMode LoadMode
        {
            get { return mLoadMode; }
        }
        /// <summary>
        /// Derived classes must override this method if they support NewItem
        /// </summary>
        public virtual void AddNew()
        {
            MessageBox.Show("Not Implemented");
        }

        public virtual bool CanAdd
        {
            get { return false; }
        }
        /// <summary>
        /// 默认视图View
        /// Derived classes can override this property if default view is not the first view in the collection
        /// </summary>
        protected virtual FolderView DefaultView
        {
            get
            {
                if (Views.Count > 0)
                {
                    return mViews[0];
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 当前内容视图控件
        /// </summary>
        public ContentViewControl CurrentContentView
        {
            get
            {
                if (this.mContentViewControl == null)
                {
                    CreateContentViewControl();
                }
                return mContentViewControl;
            }
        }
        /// <summary>
        /// 数据Provider
        /// </summary>
        public DataProvider DataProvider
        {
            get { return mDataProvider; }
        }
        /// <summary>
        /// 数据读取，读取DataProvider、ContentViewControl
        /// </summary>
        public virtual void Load()
        {
            if (mDataProvider == null)
            {
                mDataProvider = CreateDataProvider();
            }
            if (mDataProvider != null)
            {
                mDataProvider.Fill();
            }
            if (this.mContentViewControl == null)
            {
                this.CreateContentViewControl();
            }
        }
        /// <summary>
        /// 更新CurrentContentView、DataProvider
        /// </summary>
        /// <returns></returns>
        public bool UpdateData()
        {
            if (this.CurrentContentView != null)
            {
                if (!this.CurrentContentView.UpdateChanges() != null)
                {
                    return true;
                }
            }
            if (mDataProvider != null)
            {
                return mDataProvider.Update();
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 卸载CurrentContentView、DataProvider
        /// </summary>
        public virtual void Unload()
        {
            if (this.LoadMode != FolderLoadMode.Always)
            {
                if (mContentViewControl != null)
                {
                    this.ReleaseContentView();
                }
                if (mDataProvider != null)
                {
                    mDataProvider.Dispose();
                    mDataProvider = null;
                }
            }
        }
        /// <summary>
        /// 根据folderName查找ApplicationFolder
        /// </summary>
        /// <param name="folderName"></param>
        /// <returns></returns>
        public ApplicationFolder Find(string folderName)
        {
            if (string.Compare(this.FolderName, folderName, true) == 0)
            {
                return this;
            }

            ApplicationFolder folderFound = null;
            foreach (ApplicationFolder folder in this.Folders)
            {
                folderFound = folder.Find(folderName);
                if (folderFound != null)
                {
                    return folderFound;
                }
            }
            return null;
        }
        /// <summary>
        /// 保存视图配置
        /// </summary>
        /// <param name="table"></param>
        public void SaveViewSettings(ConfigDataSet.FolderViewsDataTable table)
        {
            if (mViews != null)
            {
                ConfigDataSet.FolderViewsRow row = null;

                foreach (FolderView view in this.mViews)
                {
                    row = (ConfigDataSet.FolderViewsRow)table.Rows.Find(new object[] { view.ViewName, this.GetType().FullName });
                    if (row == null)
                    {
                        row = table.AddFolderViewsRow(view.ViewName, this.GetType().FullName, System.Convert.ToInt32(view.ContentViewType), System.Convert.ToInt32(view.ReadingPaneLocation), view.LayoutName);
                    }
                    else
                    {
                        row.BeginEdit();
                        row.ContentViewType = Convert.ToInt32(view.ContentViewType);
                        row.LayoutName = view.LayoutName;
                        row.ParentFolder = this.GetType().FullName;
                        row.ReadingPaneLocation = Convert.ToInt32(view.ReadingPaneLocation);
                        row.ViewName = view.ViewName;
                        row.EndEdit();
                    }
                }
            }
            if (mFolders != null)
            {
                foreach (ApplicationFolder childFolder in this.mFolders)
                {
                    childFolder.SaveViewSettings(table);
                }
            }
        }
        /// <summary>
        /// panel的navigator
        /// </summary>
        public UIPanelBase NavigatorPanel
        {
            get
            {
                if (mNavigatorPanel == null)
                {
                    if (mParentFolder == null)
                    {
                        return null;
                    }
                    else
                    {
                        return mParentFolder.NavigatorPanel;
                    }
                }
                return mNavigatorPanel;
            }
            set { mNavigatorPanel = value; }
        }
        /// <summary>
        /// 父文件夹
        /// </summary>
        public ApplicationFolder ParentFolder
        {
            get { return mParentFolder; }
        }
        #endregion

        [Flags()]   
        public enum FolderFeatures : int
        {
            None = 0,
            MultipleViews = 1,
            ReadingPane = 2,
            NewItem = 4,
            All = 7

        }
        public enum ContentViewType : int
        {
            DashBoard,
            GridView,
            ScheduleView,
            TimelineView
        }
        public enum FolderLoadMode : int
        {
            Never,
            CurrentFolder,
            Always
        }

    }
}
